<?php

class Util {

	//检查手机号是否合法
	public function isTelephone($telephone) {
		$telephone_length = strlen($telephone);
		if (is_numeric($telephone) && $telephone_length >= 10 && $telephone_length <= 12) {
			return true;
		} else {
			return false;
		}
	}

	//计算时间，规则如下，如果一小时内，显示分钟，如果大于1小时小于1天显示小时，如果大于天且小于3天，显示天数，否则显示日期时间
	//此版本已考虑时区
	public function timeTran($the_time) {
		$now_time = date("Y-m-d H:i:s", time() + 8 * 60 * 60);
		$now_time = strtotime($now_time);
		$show_time = strtotime($the_time);
		$dur = $now_time - $show_time;
		if ($dur < 0) {
			return $the_time;
		} else {
			if ($dur < 60) {
				return '刚刚';
			} else {
				if ($dur < 3600) {
					return floor($dur / 60) . '分钟前';
				} else {
					if ($dur < 86400) {
						return floor($dur / 3600) . '小时前';
					} else {
						if ($dur < 2592000) { //30天内
							return floor($dur / 86400) . '天前';
						} else {
							return $the_time;
						}
					}
				}
			}
		}
	}

	/*
	 * httpGet
	 * 根据url获取http内容
	 * @param url 需要获取内容的http地址
	 * 返回获取结果
	 */

	public function httpGet($url) {
		$is_ssl = substr($url, 0, 8) == 'https://' ? true : false; //判断请求类型是否是https
		$cacert = 'system/config/cacert.pem';

		$curl = curl_init();
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_TIMEOUT, 500);
		
		// 为保证第三方服务器与微信服务器之间数据传输的安全性，所有微信接口采用https方式调用，必须使用下面2行代码打开ssl安全校验。
		// 如果在部署过程中代码在此处验证失败，请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
		if ($is_ssl) {
			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
			curl_setopt($curl, CURLOPT_CAINFO, $cacert); // CA根证书（用来验证的网站证书是否是CA颁布）
			curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
		}
		curl_setopt($curl, CURLOPT_URL, $url);

		$res = curl_exec($curl);
		curl_close($curl);

		return $res;
	}

	// HttpPost
	public function httpPost($url, $post_data = '') {//curl
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, 1);
		if ($post_data != '') {
			curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		}
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 500);
		curl_setopt($ch, CURLOPT_HEADER, false);

		$file_contents = curl_exec($ch);
		curl_close($ch);

		return $file_contents;
	}

	/*
	 * getClientIP
	 * 获取客户端ip
	 * @return string 返回客户端ip
	 */

	public function getClientIP() {
		if (getenv('HTTP_CLIENT_IP')) {
			$ip = getenv('HTTP_CLIENT_IP');
		} elseif (getenv('HTTP_X_FORWARDED_FOR')) {
			$ip = getenv('HTTP_X_FORWARDED_FOR');
		} elseif (getenv('HTTP_X_FORWARDED')) {
			$ip = getenv('HTTP_X_FORWARDED');
		} elseif (getenv('HTTP_FORWARDED_FOR')) {
			$ip = getenv('HTTP_FORWARDED_FOR');
		} elseif (getenv('HTTP_FORWARDED')) {
			$ip = getenv('HTTP_FORWARDED');
		} elseif(isset($_SERVER['REMOTE_ADDR'])) {
			$ip = $_SERVER['REMOTE_ADDR'];
		}
		return $ip;
	}

	/*
	 * 检测是否是合法的email地址
	 * @param email_address 待验证的email地址
	 * @return 若合法，返回true，否则返回false
	 */
	public function isEmailAddress($email_address){
		$pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
		if ( preg_match( $pattern, $email_address ) ){
			return true;
		}else{
			return false;
		}
	}
}

?>